//
//  CNActionSheet.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/7/19.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNBBSActionSheet.h"
#import <Masonry/Masonry.h>
#import <CNBaseTool/CNCategoryHeader.h>
#define CELL_HEIGHT 44.f
#define CELL_SPACE  0.0f

#define homeHeight (iPhoneX ? 34.f : 0.f)

@interface CNBBSActionSheet ()

@property (nonatomic, strong) NSArray *titleArr;

@property (nonatomic, strong) UIView  *btnBgView;

@property (nonatomic,assign,getter=isShow) BOOL  show;


@end

@implementation CNBBSActionSheet

- (instancetype)initWithTitleArray:(NSArray *)titleArr andShowCancel:(BOOL)show{
    if (self = [super init]) {
        
        self.frame = [UIScreen mainScreen].bounds;
        
        self.titleArr  = titleArr; self.show = show;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenSheet)];
        [self addGestureRecognizer:tap];
        
        [self setUpUI];
        
    }
    return self;
}
- (void)updateUIWithTitleColor:(ThemeColorPicker *)titleColor titleFont:(CGFloat)titleFont isShowSeparator:(BOOL)isShowSeparator;{
  
    for (int i = 0 ; i < self.titleArr.count+1; i++) {
        UIButton *button = (UIButton *)[self.btnBgView viewWithTag:i+1];
        
        if (titleColor) {
            [button theme_setTitleColor:titleColor forState:UIControlStateNormal];

        }
        if (titleFont > 0) {
            button.titleLabel.font = [UIFont systemFontOfSize:titleFont];

        }
        if (isShowSeparator) {
            if (i> 0 && i < self.titleArr.count) {
                UIView *lineView = [[UIView alloc] init];
                [button addSubview:lineView];
                [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(24);
                    make.right.mas_equalTo(-24);
                    make.bottom.mas_equalTo(0);
                    make.height.mas_equalTo(0.5);
                }];
                lineView.theme_backgroundColor = bbsSectionLineColorPicker;
            }
        }
        
    }
}

- (void)setUpUI{
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f];
    
    // 按钮背景
    CGSize size = [UIScreen mainScreen].bounds.size;
    
    CGFloat bgHeight;
    
    if (self.isShow) {
        bgHeight =  CELL_HEIGHT * self.titleArr.count + (CELL_HEIGHT + CELL_SPACE) + homeHeight;
    }else{
        bgHeight  = CELL_HEIGHT * self.titleArr.count + homeHeight;
    }
    
    
    
    self.btnBgView.frame = CGRectMake(0, size.height, size.width ,bgHeight);
    [self addSubview:self.btnBgView];
    
    
    // 取消
    CGFloat bgWidth = self.btnBgView.frame.size.width;
    
    UIButton  *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    
    btn.theme_backgroundColor = menuPageBGColorPicker;
    [btn theme_setTitleColor:cellTitleTextColorPicker forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.tag = self.titleArr.count + 1;
    
    btn.frame = CGRectMake(0, bgHeight - CELL_HEIGHT - homeHeight, bgWidth, CELL_HEIGHT);
    
    [self.btnBgView addSubview:btn];
    
    btn.hidden = !self.isShow;
    
    
    // 按钮
    for (int i = 0 ; i < self.titleArr.count; i++) {
        
        CGFloat btnX = 0;
        CGFloat btnY;
        
        if (self.isShow) {
            btnY = (bgHeight - CELL_HEIGHT - CELL_SPACE)  - CELL_HEIGHT*(i+1) - homeHeight;
        }else{
            btnY = bgHeight - CELL_HEIGHT*(i+1) - homeHeight;
        }
        
        
        CGFloat btnW = bgWidth;
        CGFloat btnH = CELL_HEIGHT - 0.f;
        
        UIButton  *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setTitle:self.titleArr[i] forState:UIControlStateNormal];
        
        [btn theme_setTitleColor:cellTitleTextColorPicker forState:UIControlStateNormal];
        btn.theme_backgroundColor = shareBGColorPicker;
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        
        btn.tag   = i+1;
        
        [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.btnBgView addSubview:btn];
        
    }
    
    // 显示
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.btnBgView.frame;
        frame.origin.y =  size.height - frame.size.height;
        self.btnBgView.frame = frame;
    }];
    
}

- (void)btnClickAction:(UIButton *)btn{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:clickButtonAtIndex:)]) {
        [self.delegate actionSheet:self clickButtonAtIndex:btn.tag];
    }
    
    if (self.ClickIndex) {
        self.ClickIndex(btn.tag);
    }
    [self hiddenSheet];
    
}

- (void)hiddenSheet {
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.btnBgView.frame;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height;
        self.btnBgView.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}


- (UIView *)btnBgView{
    if (!_btnBgView) {
        _btnBgView = [[UIView alloc]init];
        _btnBgView.theme_backgroundColor = menuPageBGColorPicker;
        
    }
    return _btnBgView;
}

@end
